package com.kcs.fxmargin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.model.MSetupParam;

public class SetupParameterDao {
	private Logger log = Logger.getLogger(Constant.LOG_FX);

	public MSetupParam getSetupParamter(Connection conn) throws Exception{
		
		PreparedStatement pstm 	= null;
		ResultSet rs 			= null;
		StringBuffer sql 		= new StringBuffer();
		MSetupParam mSetupParam = new MSetupParam();
		
		try{
			
			sql.append("select id, initial_margin, maintenance_margin, fx_margin_limit, active_status, create_by, create_dt, update_by, update_dt, row_version ");
			sql.append(" from m_setup_param ");
			sql.append(" where active_status ='A' ");
			
			log.debug("SQL -> " + sql.toString());
			
			pstm = conn.prepareStatement(sql.toString());
			rs   = pstm.executeQuery();
			
			while(rs.next()){
				mSetupParam.setId(rs.getString("id"));
				mSetupParam.setInitialMargin(rs.getFloat("initial_margin"));
				mSetupParam.setMaintenanceMargin(rs.getFloat("maintenance_margin"));
				mSetupParam.setFxMarginLimit(rs.getFloat("fx_margin_limit"));
				mSetupParam.setRowVersion(rs.getInt("row_version"));
			}
			
		}catch (Exception e){
			e.printStackTrace();
			
		}finally {
			if(pstm != null ) pstm.close();
			if(rs != null ) rs.close();
		}
		
		return mSetupParam;
	}
	
	public void updateSetupParameter(Connection conn, PreparedStatement pstm, MSetupParam param) throws Exception{
		
		StringBuffer sql 		= new StringBuffer();
		
		int update = 0;
		
		sql.append(" update m_setup_param set ");
//		sql.append(" initial_margin = ? , ");
//		sql.append(" maintenance_margin = ? , ");
		sql.append("  active_status = ? , ");
		sql.append("  update_by = ? , ");
		sql.append("  update_dt = getDate() , ");
		sql.append("  row_version = row_version+1 ");
		sql.append(" where id in ( ");
		sql.append("              select id from m_setup_param where active_status = 'A' ");
		sql.append("             ) ");

		log.debug("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
//		pstm.setFloat(1, param.getInitialMargin());
//		pstm.setFloat(2, param.getMaintenanceMargin());
		pstm.setString(1, param.getActiveStatus());
		pstm.setString(2, param.getUpdateBy());
//		pstm.setInt(3, param.getRowVersion());
//		pstm.setString(6, param.getId());
		
		update = pstm.executeUpdate();
		
		log.debug("update = " + update);
	}	
	
	public void insertSetupParameter(Connection conn, PreparedStatement pstm, MSetupParam param) throws Exception {
		StringBuffer sql 		= new StringBuffer();
		
		int insert = 0;
		
		sql.append(" insert into m_setup_param ");
		sql.append(" (id, initial_margin, maintenance_margin, fx_margin_limit, active_status, row_version, create_by, create_dt, update_by, update_dt) ");
		sql.append(" values (?, ?, ?, ?, ?, ?, ?, getDate(), ?, getDate()) ");
		
		log.debug("SQL -> " + sql.toString());
		
		pstm = conn.prepareStatement(sql.toString());
		pstm.setString(1, param.getId());
		pstm.setFloat(2, param.getInitialMargin());
		pstm.setFloat(3, param.getMaintenanceMargin());
		pstm.setFloat(4, param.getFxMarginLimit());
		pstm.setString(5, "A");
		pstm.setInt(6, 1);
		pstm.setString(7, param.getCreateby());
		pstm.setString(8, param.getUpdateBy());
		
		insert = pstm.executeUpdate();
		
		log.debug("insert = " + insert);
	}
}
